package cn.com.headfree.sysmenu.dao.impl;

import cn.com.headfree.sysmenu.dao.IMenuDao;
import cn.com.headfree.sysmenu.entity.TSysMenu;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.*;

@Repository("menuDao")
public class MenuDaoImpl extends BaseDaoImpl<TSysMenu> implements IMenuDao {
    private static final Logger LOG = Logger.getLogger(MenuDaoImpl.class);

    //查询全部菜单列表
    @Override
    public List<TSysMenu> findMenuList(TSysMenu sysMenu) {
        String sql = "  select t.id,t.parent_id,t.menu_name,t.menu_code,t.type,t.conent,t.path_id,t.path_name,t.is_ctrl,t.seq_number,t.status,t.res_id,t.app_id,t.domain_id,t.create_time " +
                " from t_sys_menu t " +
                " where 1=1 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);

        ArrayList<Object> arrayList = new ArrayList<Object>();
        //菜单id
        if (!StringUtil.isNullOrEmpty(sysMenu.getId())) {
            sqlBuilder.append(" AND t.id = ? ");
            arrayList.add(sysMenu.getId());
        }
        //菜单名称
        if (!StringUtil.isNullOrEmpty(sysMenu.getMenuName())) {
            sqlBuilder.append(" AND t.menu_name like concat('%',?,'%') ");
            arrayList.add(sysMenu.getMenuName());
        }
        //状态
        if (!StringUtil.isNullOrEmpty(sysMenu.getStatus())) {
            sqlBuilder.append(" AND t.status = ? ");
            arrayList.add(sysMenu.getStatus());
        }

        sqlBuilder.append(" order by t.parent_id,t.seq_number  ");
        return this.findAllBySQL(TSysMenu.class, sqlBuilder.toString(), null,false, arrayList.toArray());
    }

    //查询全部菜单列表
    @Override
    public List<TSysMenu> findMenuListByErpNo(TSysMenu sysMenu,String erpNo) {
        String sql = "  select distinct t.id,t.parent_id,t.menu_name,t.menu_code,t.type,t.conent,t.path_id,t.path_name,t.is_ctrl,t.seq_number,t.status,t.res_id,t.app_id,t.domain_id,t.create_time " +
                " from t_sys_menu t " +
                " left join t_sys_role_menu rm on t.id = rm.menu_id " +
                " left join t_sys_user_role ur on rm.role_id = ur.role_id " +
                " left join t_sys_role ro on ur.role_id = ro.id " +
                " where 1=1 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);

        ArrayList<Object> arrayList = new ArrayList<Object>();
        //人资编号
        if (!StringUtil.isNullOrEmpty(erpNo)) {
            sqlBuilder.append(" AND ur.erp_no = ? ");
            arrayList.add(erpNo);
        }
        //菜单id
        if (!StringUtil.isNullOrEmpty(sysMenu.getId())) {
            sqlBuilder.append(" AND t.id = ? ");
            arrayList.add(sysMenu.getId());
        }
        //菜单名称
        if (!StringUtil.isNullOrEmpty(sysMenu.getMenuName())) {
            sqlBuilder.append(" AND t.menu_name like concat('%',?,'%') ");
            arrayList.add(sysMenu.getMenuName());
        }
        //状态
        if (!StringUtil.isNullOrEmpty(sysMenu.getStatus())) {
            sqlBuilder.append(" AND t.status = ? ");
            arrayList.add(sysMenu.getStatus());
        }

        sqlBuilder.append(" order by t.parent_id,t.seq_number  ");
        return this.findAllBySQL(TSysMenu.class, sqlBuilder.toString(), null,false, arrayList.toArray());
    }

    //根据菜单名称查询菜单全部列表
    @Override
    public List<TSysMenu> findMenuListByMenuName(String menuId, String menuName) {
        String sql = "  select t.id,t.parent_id,t.menu_name,t.menu_code,t.type,t.conent,t.path_id,t.path_name,t.is_ctrl,t.seq_number,t.status,t.res_id,t.app_id,t.domain_id,t.create_time " +
                " from t_sys_menu t " +
                " where 1=1 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);

        ArrayList<Object> arrayList = new ArrayList<Object>();
        //菜单id
        if (!StringUtil.isNullOrEmpty(menuId)) {
            sqlBuilder.append(" AND t.id = ? ");
            arrayList.add(menuId);
        }
        //菜单名称
        if (!StringUtil.isNullOrEmpty(menuName)) {
            sqlBuilder.append(" AND t.menu_name = ? ");
            arrayList.add(menuName);
        }

        sqlBuilder.append(" order by t.parent_id,t.seq_number  ");
        return this.findAllBySQL(TSysMenu.class, sqlBuilder.toString(), null,false, arrayList.toArray());
    }

    //加载对应角色菜单列表
    @Override
    public List<TSysMenu> selectMenuListByRoleId(String roleId) {
        String sql = "  select m.id,m.parent_id,m.menu_name,m.menu_code,m.type,m.conent,m.path_id,m.path_name,m.is_ctrl,m.seq_number,m.status,m.res_id,m.app_id,m.domain_id,m.create_time " +
                " from t_sys_menu m " +
                " left join t_sys_role_menu rm on m.id = rm.menu_id " +
                " where 1=1 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);

        ArrayList<Object> arrayList = new ArrayList<Object>();
        //菜单id
        if (!StringUtil.isNullOrEmpty(roleId)) {
            sqlBuilder.append(" AND rm.role_id = ? ");
            arrayList.add(roleId);
        }
        // and m.id not in (select m.parent_id from t_sys_menu m inner join t_sys_role_menu rm on m.id = rm.menu_id and rm.role_id = 'tye4866ac325485e9a517f8cbcd738d4')
        sqlBuilder.append(" order by m.parent_id, m.seq_number  ");
        return this.findAllBySQL(TSysMenu.class, sqlBuilder.toString(), null,false, arrayList.toArray());
    }

    //加载对应角色菜单列表
    @Override
    public List<TSysMenu> selectMenuListByParentId(String parentId) {
        String sql = "  select m.id,m.parent_id,m.menu_name,m.menu_code,m.type,m.conent,m.path_id,m.path_name,m.is_ctrl,m.seq_number,m.status,m.res_id,m.app_id,m.domain_id,m.create_time " +
                " from t_sys_menu m " +
                " where 1=1 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);

        ArrayList<Object> arrayList = new ArrayList<Object>();
        //菜单id
        if (!StringUtil.isNullOrEmpty(parentId)) {
            sqlBuilder.append(" AND m.parent_id = ? ");
            arrayList.add(parentId);
        }
        // and m.id not in (select m.parent_id from t_sys_menu m inner join t_sys_role_menu rm on m.id = rm.menu_id and rm.role_id = 'tye4866ac325485e9a517f8cbcd738d4')
        sqlBuilder.append(" order by m.parent_id, m.seq_number  ");
        return this.findAllBySQL(TSysMenu.class, sqlBuilder.toString(), null,false, arrayList.toArray());
    }

    //新增菜单
    @Override
    public void addMenu(TSysMenu sysMenu) {
        String sql = "insert into t_sys_menu (id,parent_id,menu_name,menu_code,type,conent,path_id,path_name,is_ctrl,seq_number,status,res_id,app_id,domain_id,create_time) values ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append("(#{id,jdbcType=VARCHAR},").append("#{parent_id,jdbcType=VARCHAR},")
                .append("#{menu_name,jdbcType=VARCHAR},").append("#{menu_code,jdbcType=VARCHAR},")
                .append("#{type,jdbcType=VARCHAR},").append("#{conent,jdbcType=VARCHAR},")
                .append("#{path_id,jdbcType=VARCHAR},").append("#{path_name,jdbcType=VARCHAR},")
                .append("#{is_ctrl,jdbcType=VARCHAR},").append("#{seq_number,jdbcType=INTEGER},")
                .append("#{status,jdbcType=VARCHAR},").append("#{res_id,jdbcType=VARCHAR},")
                .append("#{app_id,jdbcType=VARCHAR},").append("#{domain_id,jdbcType=VARCHAR},")
                .append("#{create_time,jdbcType=DATE})");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", IdGen.uuid());
        params.put("parent_id", sysMenu.getParentId());
        params.put("menu_name", sysMenu.getMenuName());
        params.put("menu_code", sysMenu.getMenuCode());
        params.put("type", sysMenu.getType());
        params.put("conent", sysMenu.getConent());
        params.put("path_id", sysMenu.getPathId());
        params.put("path_name", sysMenu.getPathName());
        params.put("is_ctrl", sysMenu.getIsCtrl());
        params.put("seq_number", sysMenu.getSeqNumber());
        params.put("status", sysMenu.getStatus());
        params.put("res_id", sysMenu.getResId());
        params.put("app_id", sysMenu.getAppId());
        params.put("domain_id", sysMenu.getDomainId());
        params.put("create_time", new Timestamp(new Date().getTime()));
        this.executeSql(sqlBuilder.toString(), params);
    }

    //更新菜单
    @Override
    public void updateMenu(TSysMenu sysMenu) {
        String sql = "update t_sys_menu set ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append("parent_id=#{parent_id,jdbcType=VARCHAR},")
                .append("menu_name=#{menu_name,jdbcType=VARCHAR},")
                .append("menu_code=#{menu_code,jdbcType=VARCHAR},")
                .append("type=#{type,jdbcType=VARCHAR},")
                .append("conent=#{conent,jdbcType=VARCHAR},")
                .append("path_id=#{path_id,jdbcType=VARCHAR},")
                .append("path_name=#{path_name,jdbcType=VARCHAR},")
                .append("is_ctrl=#{is_ctrl,jdbcType=VARCHAR},")
                .append("seq_number=#{seq_number,jdbcType=INTEGER},")
                .append("status=#{status,jdbcType=VARCHAR},")
                .append("res_id=#{res_id,jdbcType=VARCHAR},")
                .append("app_id=#{app_id,jdbcType=VARCHAR},")
                .append("domain_id=#{domain_id,jdbcType=VARCHAR}");
        sqlBuilder.append(" where 1=1 ");
        sqlBuilder.append(" and id = #{id,jdbcType=VARCHAR} ");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", sysMenu.getId());
        params.put("parent_id", sysMenu.getParentId());
        params.put("menu_name", sysMenu.getMenuName());
        params.put("menu_code", sysMenu.getMenuCode());
        params.put("type", sysMenu.getType());
        params.put("conent", sysMenu.getConent());
        params.put("path_id", sysMenu.getPathId());
        params.put("path_name", sysMenu.getPathName());
        params.put("is_ctrl", sysMenu.getIsCtrl());
        params.put("seq_number", sysMenu.getSeqNumber());
        params.put("status", sysMenu.getStatus());
        params.put("res_id", sysMenu.getResId());
        params.put("app_id", sysMenu.getAppId());
        params.put("domain_id", sysMenu.getDomainId());
        this.executeSql(sqlBuilder.toString(), params);
    }

    //批量删除菜单
    @Override
    public void deleteMenu(String[] menuIds) {
        String sql = "delete from t_sys_menu ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" where 1=1 ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and id in ('").append(String.join("','",menuIds)).append("') ");
        this.executeSql(sqlBuilder.toString(), params);
    }

    //删除菜单
    @Override
    public void deleteMenuById(String menuId) {
        String sql = "delete from t_sys_menu ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" where 1=1 ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and id = #{id,jdbcType=VARCHAR} ");
        params.put("id", menuId);
        this.executeSql(sqlBuilder.toString(), params);
    }
}
